package ah.file;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class InsertStr {

	/**
	 * @param args
	 * 批量给文件插入一字符串
	 */
	public static void main(String[] args) {
		
		String path = "C:\\workHtml";//文件目录
		File file = new File(path);

		File[] dirs = file.listFiles();
		//插入的字符串
		String str = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
		char[] str_char = str.toCharArray();
		
		for (File dir : dirs) {
			if (dir.isDirectory() == true) {
				File[] files = dir.listFiles();

				for (File f : files) {
					
					if (f.isFile()) {
						try {
							
							StringBuffer strBuff = new StringBuffer();

							BufferedReader in = new BufferedReader(
									new InputStreamReader(
											new FileInputStream(f), "UTF-8"));

							char[] cbuf = new char[1000];
							int a = 0;
							a = in.read(cbuf);
							strBuff.append(cbuf, 0, a);
							//插入字符串位置
							int index = strBuff.indexOf("</title>");
							strBuff.insert(index+8, str, 0, str_char.length);
							
							a = 0;
							while ((a = in.read(cbuf)) > 0) {
								strBuff.append(cbuf, 0, a);
							}
							in.close();
							
							
							//System.out.println(strBuff.toString());
							Writer out = new BufferedWriter(
									new OutputStreamWriter(
											new FileOutputStream(f), "UTF-8"));
							out.write(strBuff.toString());
							out.close();
						} catch (Exception e) {

							e.printStackTrace();
						}
					}
				}
			}

		}

	}
	
	
	private Object[] getEncodingName(byte[] b4, int count) throws Exception {   
        if (count < 2) {   
            throw new Exception("unknow");   
        }   

        // UTF-16, with BOM   
        int b0 = b4[0] & 0xFF;   
        int b1 = b4[1] & 0xFF;   
        if (b0 == 0xFE && b1 == 0xFF) {   
            // UTF-16, big-endian   
            return new Object[]{"UTF-16BE",new Integer(2)};   
        }   
        if (b0 == 0xFF && b1 == 0xFE) {   
            // UTF-16, little-endian   
            return new Object[] {"UTF-16LE",new Integer(2)};   
        }   

        // default to UTF-8 if we don't have enough bytes to make a   
        // good determination of the encoding   
        if (count < 3) {   
            throw new Exception("unknow");   
        }   

        // UTF-8 with a BOM   
        int b2 = b4[2] & 0xFF;   
        if (b0 == 0xEF && b1 == 0xBB && b2 == 0xBF) {   
            return new Object[]{"utf-8",new Integer(3)};   
        }   

        // default to UTF-8 if we don't have enough bytes to make a   
        // good determination of the encoding   
        if (count < 4) {   
            throw new Exception("unknow");   
        }   

        // other encodings   
        int b3 = b4[3] & 0xFF;   
        if (b0 == 0x00 && b1 == 0x00 && b2 == 0x00 && b3 == 0x3C) {   
            // UCS-4, big endian (1234)   
            return new Object[]{"ISO-10646-UCS-4",new Integer(4)};   
        }   
        if (b0 == 0x3C && b1 == 0x00 && b2 == 0x00 && b3 == 0x00) {   
            // UCS-4, little endian (4321)   
            return new Object[]{"ISO-10646-UCS-4",new Integer(4)};   
        }   
        if (b0 == 0x00 && b1 == 0x00 && b2 == 0x3C && b3 == 0x00) {   
            // UCS-4, unusual octet order (2143)   
            // REVISIT: What should this be?   
            return new Object[]{"ISO-10646-UCS-4",new Integer(4)};   
        }   
        if (b0 == 0x00 && b1 == 0x3C && b2 == 0x00 && b3 == 0x00) {   
            // UCS-4, unusual octect order (3412)   
            // REVISIT: What should this be?   
            return new Object[]{"ISO-10646-UCS-4",new Integer(4)};   
        }   
        if (b0 == 0x00 && b1 == 0x3C && b2 == 0x00 && b3 == 0x3F) {   
            // UTF-16, big-endian, no BOM   
            // (or could turn out to be UCS-2...   
            // REVISIT: What should this be?   
            return new Object[]{"UTF-16BE",new Integer(4)};   
        }   
        if (b0 == 0x3C && b1 == 0x00 && b2 == 0x3F && b3 == 0x00) {   
            // UTF-16, little-endian, no BOM   
            // (or could turn out to be UCS-2...   
            return new Object[]{"UTF-16LE",new Integer(4)};   
        }   
        if (b0 == 0x4C && b1 == 0x6F && b2 == 0xA7 && b3 == 0x94) {   
            // EBCDIC   
            // a la xerces1, return CP037 instead of EBCDIC here   
            return new Object[]{"CP037",new Integer(4)};   
        }   

        throw new Exception("unknow");   

    }

}
